<?php
namespace Mob\Controller;
import('ORG.Util.Date');
use Org\Util\Date;
/**
 * 个人中心概览控制器
 * @author Administrator
 *
 */
class SurveyController extends MobBaseController
{

    public function index(){
        if (null == session('uid')) {
            $this->display("login/index");
            exit();
        }
            $database_user_assets = D('user_assets');
            $condition['uid'] = session('uid');
            $user_assets = $database_user_assets->field(true)->where($condition)->find();
            $totalMoney=$user_assets["avail_money"]+$user_assets["locked_money"];
            //所有域名
            $total = D('UserDomains')->get_domains_count(session('uid'),null,null,null);
            
            //审核通过的域名
            $checkCount = D('UserDomains')->get_domains_count(session('uid'),3,null,null);
            //审核通过域名信息
            $checkList=D('UserDomains')->get_domains(session('uid'),3,null,null);
            //最高和最低估价
            $high=$min=0;
            foreach($checkList as $k=>$val){
                $high = $high+$val['domainType']['ym_news_price'];
            };
            
            //委单中域名
            $inOrderCount = D('UserDomains')->get_domains_count(session('uid'),3,2,"trans_status");
            
            //转出中域名
            $transOutCount = D('UserDomains')->get_domains_count(session('uid'),3,3,"trans_status");
            
            //抵押中域名
            $mortageCount = D('UserDomains')->get_domains_count(session('uid'),3,2,"is_mortage");
            
            //出借中域名
            $lendCount = D('UserDomains')->get_domains_count(session('uid'),3,2,"is_lend");
            //交易记录
            $entrustList = D('UserEntrust')->getList(array('uid' => session('uid'),'status'=>0));
            $entrustCount = D('UserEntrust')->getCount(array('uid' => session('uid'),'status'=>0));
            
            $database_user = D('user');
            $condition_user['id'] = session('uid');
            $user = $database_user->field(true)->where($condition_user)->find();
            
            $sig["number"] = $user['signumber'];
            $sig["status"] = 1 ;
            if($this->DateDiff("d",Date('Y-m-d',strtotime($user['sigdate'])),Date('Y-m-d')) != 0){
                $sig["status"] = 0;
            };
            $this->assign('sig', $sig);
            $this->assign('userAssets', $user_assets);
            $this->assign('totalMoney', $totalMoney);
            $this->assign('total', $total);
            $this->assign('checkCount', $checkCount);
            $this->assign('inOrderCount', $inOrderCount);
            $this->assign('transOutCount', $transOutCount);
            $this->assign('mortageCount', $mortageCount);
            $this->assign('normalCount', $checkCount-$inOrderCount-$transOutCount-$mortageCount-$lendCount);
            $this->assign('high', $high);
            $this->assign('list', $entrustList);
            $this->assign('count', $entrustCount);
            $this->display('survey/index');
    }
    
    /**
     * 签到
     */
    public function signIn(){
        $resp['status']=0;
        $resp['msg']="签到不成功！";
        $database_user = D('user');
        $condition['id'] = session('uid');
        $user = $database_user->field(true)->where($condition)->find();
        if($this->DateDiff("d",Date('Y-m-d',strtotime($user['sigdate'])),Date('Y-m-d')) != 0){
            $arry=array('uid' => session('uid'),'operation' => 3,'client_type' => 1);
            $return = D('UserOperalog')->inst_userOperaLogs($arry);
            if(!$return['status'] == 1){
                $resp['msg']=$return['mess'];
            }else{
                $data['sigdate'] = Date('Y-m-d H:i:s');
                $database_user->where($condition)->setInc('signumber');
                $result = $database_user->where($condition)->save($data);
                if($result !== false){
                    $resp['status']=1;
                    $resp['msg']="签到成功！";
                };
            }
        }else{
            $resp['msg']="当日不能重复签到！";
        };
        $this->ajaxReturn($resp,'JSON');
    }
 
    public function assetcredit(){
        if (null == session('uid')) {
            $this->ajaxReturn(array('code'=>'error','info' => '请先登录！'));//0错误，1成功
            exit;
        }
        $type=I('typed','');
        $authpwd=I('authpwd','');
        $transUser=M('User')->field('transpasswd')->find(session('uid'));
        if($transUser['transpasswd']!=md5($authpwd)){
            $this->ajaxReturn(array('code'=>'error','info' => '支付密码错误！'));//0错误，1成功
            exit;
        }
        $money=I('money',0);
        is_numeric($money);
        if(!preg_match('/^\\d+$/',$money)) {
            $this->ajaxReturn(array('code'=>'error','info' => '输入的金额有误！'));//0错误，1成功
            exit;
        }
        $UserAssets=M('UserAssets')->field('avail_money,present_money,notpresent_money,creditmoney')->where('uid='.session('uid'))->select();
        if($type==1){//转入理财宝
            if($money==0 || $UserAssets[0]['avail_money']<$money){
                $this->ajaxReturn(array('code'=>'error','info' => '账户可用资金不足！'));//0错误，1成功
                exit;
            }
            if($UserAssets[0]['notpresent_money']>=$money){
                M('UserAssets')->where('uid='.session('uid'))->setDec('notpresent_money',$money);
            }else {
                M('UserAssets')->where('uid='.session('uid'))->setField(array('notpresent_money'=>0));
                M('UserAssets')->where('uid='.session('uid'))->setDec('notpresent_money',$money-$UserAssets[0]['notpresent_money']);
            }
            M('UserAssets')->where('uid='.session('uid'))->setDec('avail_money',$money);
            M('UserAssets')->where('uid='.session('uid'))->setInc('creditmoney',$money);
        }else{//转出
            if($money==0 || $UserAssets[0]['creditmoney']<$money){
                $this->ajaxReturn(array('code'=>'error','info' => '最多能够转出！'.$UserAssets[0]['creditmoney']));//0错误，1成功
                exit;
            }
            M('UserAssets')->where('uid='.session('uid'))->setInc('avail_money',$money);
            M('UserAssets')->where('uid='.session('uid'))->setInc('notpresent_money',$money);
            M('UserAssets')->where('uid='.session('uid'))->setDec('creditmoney',$money);
        }
        $this->ajaxReturn(array('code'=>'success','info' => '账户可用资金不足！'));//0错误，1成功
        exit;
    }
}